package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.feign.service.PositionFeignService;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.GetPositionDetailVo;
import com.alibaba.algo.cooperation.feign.vo.req.SelectPositionListRequestVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectPositionUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.*;
import com.alibaba.algo.cooperation.usercomponent.service.user.SysPositionService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserPositionService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.PositionInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

//import com.alibaba.algo.cooperation.usercomponent.service.user.SysPositionService;

/**
　　* @description: 用户岗位服务
　　* @author shenrm
　　* @date 2021/11/2 18:57
　　*/
@Slf4j
@Service
public class UserPositionServiceImpl implements UserPositionService {

    @Autowired
    private SysPositionService sysPositionService;

    @Autowired
    private PositionFeignService positionFeignService;

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public List<PositionInfo> getAllPositions() {
        BaseResult positions = sysPositionService.qryAllPositions();
        if(Objects.isNull(positions)) {
            return Collections.emptyList();
        }
        List<PositionInfo> positionInfos = new ArrayList<>();
        List<LinkedHashMap> listMap = (List<LinkedHashMap>) positions.getData();
        PositionInfo positionInfo;
        for (LinkedHashMap map : listMap){
            positionInfo = new PositionInfo();
            positionInfo.setPositionName(String.valueOf(map.get("name")));
            positionInfo.setPositionCode(String.valueOf(map.get("id")));
            positionInfos.add(positionInfo);
        }
        return positionInfos;
    }

    @Override
    public List<UserInfo> getUserListByPositionId(String positionCode) {
        List<Long> positionIds = new ArrayList<>();
        positionIds.add(Long.parseLong(positionCode));
        SelectPositionUserReqVO reqVO = new  SelectPositionUserReqVO();
        reqVO.setPositionIdList(positionIds);
        BaseResult<List<PositionUserInfoResVO>> positionUserInfos = positionFeignService.qryUserListByPositionId(reqVO);
        if(Objects.isNull(positionUserInfos)) {
            return Collections.emptyList();
        }

        List<UserInfo> userInfos = new ArrayList<>();
        List<PositionUserInfoResVO> positionUserInfoResVOS = positionUserInfos.getData();
        UserInfo userInfo;
        for (PositionUserInfoResVO vo : positionUserInfoResVOS){
            if (Objects.nonNull(vo.getUserId())){
                userInfo = new UserInfo();
                userInfo.setUserCode(String.valueOf(vo.getUserId()));
                userInfo.setUserName(vo.getUserName());
                userInfos.add(userInfo);
            }
        }
        return userInfos;
    }

    @Override
    public List<PositionInfo> getPositionInfoListByUserId(String userId) {
        SelectUserInfoReqVO reqVO = new SelectUserInfoReqVO();
        reqVO.setUserId(Long.parseLong(userId));
        BaseResult<RpcUserInfoVO> rpcUserInfoVos = userFeignService.qryUserInfo(reqVO);
        if(Objects.isNull(rpcUserInfoVos)) {
            return Collections.emptyList();
        }
        RpcUserInfoVO rpcUserInfoVO = rpcUserInfoVos.getData();
        if(Objects.isNull(rpcUserInfoVO) ||
                Objects.isNull(rpcUserInfoVO.getPositionList())) {
            return Collections.emptyList();
        }

        // 岗位集合
        List<PositionInfo> positionInfos = new ArrayList<>();
        PositionInfo positionInfo;
        for (UserPositionResVO vo : rpcUserInfoVO.getPositionList()){
            positionInfo = new PositionInfo();
            positionInfo.setPositionCode(String.valueOf(vo.getPositionId()));
            positionInfo.setPositionName(vo.getPositionName());
            positionInfos.add(positionInfo);
        }
        return positionInfos;
    }

    @Override
    public List<UserInfo> getUserListByPositionCodes(List<String> positionCodes) {
        List<UserInfo> userInfos = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(positionCodes)) {
            List<Long> positionCodeList = positionCodes.stream().map(positionCode -> {
                return Long.parseLong(positionCode);
            }).collect(Collectors.toList());

            SelectPositionUserReqVO reqVO = new SelectPositionUserReqVO();
            reqVO.setPositionIdList(positionCodeList);
            BaseResult<List<PositionUserInfoResVO>> positionUserInfos = positionFeignService.selectUserListByPosition(reqVO);
            if(Objects.isNull(positionUserInfos)) {
                return Collections.emptyList();
            }

            List<PositionUserInfoResVO> positionUserInfoResVOS = positionUserInfos.getData();
            UserInfo userInfo;
            for (PositionUserInfoResVO vo : positionUserInfoResVOS){
                if (Objects.nonNull(vo.getUserId())){
                    userInfo = new UserInfo();
                    userInfo.setUserCode(String.valueOf(vo.getUserId()));
                    userInfo.setUserName(vo.getUserName());
                    userInfos.add(userInfo);
                }
            }
        }
        return userInfos;
    }

    /**
     * 根据组织名称查询分页组织集合数据列表
     *
     * @param positionName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult getUserPositionPageList(String positionName, Integer pageNo, Integer pageSize) {
        List<PositionInfo> positionInfoList = new ArrayList<>();
        SelectPositionListRequestVO vo = new SelectPositionListRequestVO();
        if (StringUtils.isNotBlank(positionName) && StringUtils.isNotEmpty(positionName)) {
            vo.setKeyName(positionName);
        }
        vo.setPageIndex(pageNo);
        vo.setPageSize(pageSize);
        Long totalCount = 0L;
        BaseResult<PagerEntity<SelectPositionListResponseVO>> dataList = positionFeignService.selectPage(vo);
        if (dataList!=null && dataList.getData()!=null && dataList.getData().getDataList()!=null) {
            for (SelectPositionListResponseVO entity : dataList.getData().getDataList()) {
                PositionInfo pvo = new PositionInfo();
                pvo.setPositionCode(entity.getId().toString());
                pvo.setPositionName(entity.getName());
                positionInfoList.add(pvo);
            }
            totalCount = dataList.getData().getTotal();
        }
        return ResponseResult.success(positionInfoList)
                .setTotalCount(totalCount.intValue())
                .setPageNum(pageNo)
                .setPageSize(pageSize);
    }

    @Override
    public PositionInfo getPositionDetail(Long positionCode) {
        if(Objects.isNull(positionCode)) {
            return null;
        }
        GetPositionDetailVo detailVo = new GetPositionDetailVo();
        detailVo.setId(positionCode);
        BaseResult<FeignPositionDetailVo> result;
        try {
            result = positionFeignService.getPositionDetail(detailVo);
        } catch (Exception ex) {
            log.error("查询岗位明细信息失败,失败原因:{}", ex.getMessage(), ex);
            return null;
        }
        FeignPositionDetailVo positionDetailVo = result.getData();
        if(Objects.nonNull(positionDetailVo)){
            PositionInfo positionInfo = new PositionInfo();
            positionInfo.setPositionCode(String.valueOf(positionCode));
            positionInfo.setPositionName(positionDetailVo.getName());
            positionInfo.setCode(positionDetailVo.getPosCode());
            return positionInfo;
        }
        return null;
    }

    @Override
    public PositionInfo getPositionDetailByPosCode(String positionCode) {
        if(StringUtils.isBlank(positionCode)) {
            return null;
        }
        GetPositionDetailVo detailVo = new GetPositionDetailVo();
        detailVo.setPosCode(positionCode);
        BaseResult<FeignPositionDetailVo> result;
        try {
            result = positionFeignService.getPositionDetail(detailVo);
        } catch (Exception ex) {
            log.error("查询岗位明细信息失败,失败原因:{}", ex.getMessage(), ex);
            return null;
        }
        FeignPositionDetailVo positionDetailVo = result.getData();
        if(Objects.nonNull(positionDetailVo)){
            PositionInfo positionInfo = new PositionInfo();
            positionInfo.setPositionCode(String.valueOf(positionDetailVo.getId()));
            positionInfo.setPositionName(positionDetailVo.getName());
            positionInfo.setCode(positionDetailVo.getPosCode());
            return positionInfo;
        }
        return null;
    }
}